﻿var productList = "", productCountList = "";

function Delete(productId) {
    if (confirm("确认要删除该商品?")) {
        GetShoppingCarInfo(productId);
        if (productList == "" || productCountList == "") {
            $.post("prodlist.aspx?action=clear",
				function(result) {
				    if (result == "success") {
				        location.href = document.location;
				    }
				})
        }
        else {
            $.get("prodlist.aspx",
					{ action: "del", productList: productList, productCountList: productCountList, id: productId },
					function(result) {
					    if (result == "success") {
					        alert("删除成功");
					        location.href = document.location;
					    }
					    else
					        alert("删除失败,请同管理员联系。");
					})
        }
    }
}
/**************************************
***获取当前购物车中的信息***
**************************************/
function GetShoppingCarInfo(productId) {
    productList = "", productCountList = "";
    var count = document.getElementsByName("txt_Count").length;
    for (var i = 0; i < count; i++) {
        if (productId == document.getElementsByName("productId").item(i).value)
            continue;
        if (productList == "" && productCountList == "") {
            productList = document.getElementsByName("productId").item(i).value;
            productCountList = document.getElementsByName("txt_Count").item(i).value;
        }
        else {
            productList += "," + document.getElementsByName("productId").item(i).value;
            productCountList += "," + document.getElementsByName("txt_Count").item(i).value;
        }
    }

}
function GoStep2(productId) {
    GetShoppingCarInfo(0);
    if (productList == "" || productCountList == "") {
        if (confirm("您没有选购任何商品,是否现在去选购商品?"))
            location.replace("index.html");
        else
            return;
    }
    else {
        /*
        ValidateConsigneeInfo();//判断收货地址信息是否完整
        if(g_ValidateField != "")
        {
        alert("收获地址输入不完整,请输入。");
		
				return;
        }
        */

        var jv1 = $('#txt_Invoice');
        if (jv1.val() == "") {
            alert("发票抬头不允许为空!");
            jv1.focus();
            return;
        }

        jv1 = $('#txt_Consignee');
        if (jv1.val() == "") {
            alert("收货联系人不允许为空!");
            jv1.focus();
            return;
        }

        jv1 = $('#txt_Address');
        if (jv1.val() == "") {
            alert("联系地址不允许为空!");
            jv1.focus();
            return;
        }

        jv1 = $('#txt_Zip');
        if (!IsZip(jv1.val())) {
            alert("邮政编码输入格式错误!");

            jv1.focus();
            return;
        }

        jv1 = $('#txt_Telphone');
        if (!IsNumeric(jv1.val())) {
            alert("联系电话输入格式错误!");
            jv1.focus();
            return;
        }
        jv1 = $('#txt_Mobile');
        if (!IsMobile(jv1.val())) {
            alert("联系手机输入格式错误!");
            jv1.focus();
            return;
        }


        if ($("#hdn_PayType").val() == "") {
            alert("请选择付款方式");
        }
        else {
            $("#hdn_ProductList").val(productList);
            $("#hdn_ProductCountList").val(productCountList);
            $("#mainForm")[0].submit();
        }
    }
}
function ClearShoppingCar() {
    if (confirm("确认要清除购物车中的所有商品?")) {
        $.post("prodlist.aspx?action=clear",
				function(result) {
				    if (result == "success") {
				        location.href = document.location;
				    }
				})
    }
}

function UpdateShoppingCar(obj, data) {
    var count, preCount, productId;
    var sign = false; //标记当前购物车的数据有没有变化
    count = data.split('$')[0]; //当前输入的商品数量
    preCount = data.split('$')[1]; //之前的商品数量
    productId = data.split('$')[2]; //商品ID
    if (count == '0' || count == '') {
        if (confirm("确定不购买该商品?")) {
            GetShoppingCarInfo(productId);
            sign = true;
        }
        else
            obj.value = preCount;
    }
    else {
        GetShoppingCarInfo(0);
        sign = true;
    }
    if (sign) {
        $.post("prodlist.aspx?action=reload&productList=" + productList + "&productCountList=" + productCountList,
        //{action:"reload",productList:productList,productCountList:productCountList},
			function(result) {
			    location.href = document.location;
			})
    }
}

/**************数字验证**************/
function IsNumeric(value) {
    var pattern = /^[0-9]+$/;
    if (pattern.test(value))
        return true;
    return false;

}
/**************手机号验证**************/
function IsMobile(value) {
    var pattern = /^(13[0-9]|15[0-9])\d{8}$/;
    if (pattern.test(value))
        return true;
    return false;
}
/**************邮编验证**************/
function IsZip(value) {
    var pattern = /^\d{6}$/;
    if (pattern.test(value))
        return true;
    return false;
}

/**************************************
***收获地址信息验证***
**************************************/
var g_ValidateField = ""; //全局标签ID
function CommonValidate() {
    var obj = arguments[0];
    var validateField = obj.id; //当前要验证的标签ID
    //如果当前要验证的标签ID同全局标签ID是否相同
    //如果相同则继续验证，否则退出当前验证以避免进入死循环。
    if (g_ValidateField != "" && g_ValidateField != validateField)
        return;
    g_ValidateField = validateField;
    if (obj.value == "") {
        alert(obj.title + "不允许为空。")
        obj.focus();
        obj.select();
        return false;
    }
    if (arguments.length > 1) {
        var type = arguments[1];
        switch (type) {
            case "Number":
                if (!IsNumeric(obj.value)) {
                    alert(obj.title + "输入格式错误");
                    obj.focus();
                    obj.select();
                    return false;
                }
                break;
            case "Zip":
                if (!IsZip(obj.value)) {
                    alert(obj.title + "输入格式错误");
                    obj.focus();
                    obj.select();
                    return false;
                }
                break;
            case "Mobile":
                if (!IsMobile(obj.value)) {
                    alert(obj.title + "输入格式错误");
                    obj.focus();
                    obj.select();
                    return false;
                }
                break;
        }
    }
    g_ValidateField = "";
    return;
}
/**************************************
***收获地址信息判断***
**************************************/
function ValidateConsigneeInfo() {
    var i = 0;
    var tags = document.getElementsByTagName("input");
    var count = tags.length;
    var m_obj;
    for (i = 0; i < count; i++) {
        m_obj = tags[i];
        if (m_obj.title != "" && m_obj.title != undefined && m_obj.value == "") {
            g_ValidateField = m_obj.id;
            break;
        }
    }
}